﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;



namespace CTEA_INDIA
{
    public partial class ViewAllCandidate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;

           
            //int userTypeId = Convert.ToInt32(Session["UserTypeId"]);

            if (!IsPostBack)
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("Default.aspx");
                }
                 
                    BAL.Customers.PopulateLocation(ref ddlLocation);
                ddlLocation.SelectedValue = Session["Location"].ToString();
                if (Session["UserTypeId"].ToString() == "3")
                {
                    gdCandidateProfile.Columns[1].Visible = false;
                    gdCandidateProfile.Columns[2].Visible = false;
                    gdCandidateProfile.Columns[5].Visible = false;
                    gdCandidateProfile.Columns[6].Visible = false;
                    PopulateCandidateGrid();

                }
                if (Session["UserTypeId"].ToString() == "1")
                {
                    ddlLocation.Enabled = true;
                    PopulateCandidateGrid();
                }
                if (Session["UserTypeId"].ToString() == "2")
                {

                    PopulateCandidateGrid();
                }
            }
        }

        public void PopulateCandidateGrid()
        {
            string  locationID =  ddlLocation.Text;

            DataSet ds = BAL.Customers.ViewAllCandidate(locationID);
            gdCandidateProfile.DataSource = ds;
            gdCandidateProfile.DataBind();

        }

        protected void gdCandidateProfile_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lEdit = (LinkButton)e.Row.FindControl("lnkEdit");
                Button update = (Button)e.Row.FindControl("btnUpdate");
                LinkButton lDelete = (LinkButton)e.Row.FindControl("lnkDelete");
               // lEdit.CommandArgument = e.Row.RowIndex.ToString();
               // update.CommandArgument = e.Row.RowIndex.ToString();
                lDelete.CommandArgument = e.Row.RowIndex.ToString();


              //  Panel gvPanel = (Panel)e.Row.FindControl("pnlLnkView");
               // Label lblAge = (Label)gvPanel.FindControl("lblAge");

                //if (Session["UserTypeId"].ToString() == "3")
                //{
                //    HtmlTableRow trMob = (HtmlTableRow)gvPanel.FindControl("trMobEmail");
                //    trMob.Visible = false;

                //    HtmlTableRow trAd = (HtmlTableRow)gvPanel.FindControl("trAdd");
                //    trAd.Visible = false;

                //    HtmlTableRow trGnm = (HtmlTableRow)gvPanel.FindControl("trGnmLoc");
                //    trGnm.Visible = false;

                //    HtmlTableRow trGco = (HtmlTableRow)gvPanel.FindControl("trGcoRel");
                //    trGco.Visible = false;

                //    HtmlTableRow trLoc = (HtmlTableRow)gvPanel.FindControl("trLocQua");
                //    trLoc.Visible = false;

                //}


            }
        }

        protected void lnkDelete_Click2(object sender, EventArgs e)
        {
            LinkButton lnkDel = (LinkButton)sender;
            int rowIndex = Convert.ToInt32(lnkDel.CommandArgument);

            int candidateId = Convert.ToInt32(gdCandidateProfile.DataKeys[rowIndex]["candidateId"]);

            BAL.Customers.IsDeleteCandidate(candidateId);

            PopulateCandidateGrid();
        }

        protected void lnkView_Click2(object sender, EventArgs e)
        {
            LinkButton lnkVw = sender as LinkButton ;
            GridViewRow   row = (GridViewRow )lnkVw.Parent.Parent ;
            string locationID = ddlLocation.Text;
         

            int Cid = Convert.ToInt32(gdCandidateProfile.DataKeys[row.RowIndex]["candidateId"]);


            string[] Details = BAL.Customers.GetCandidateById(Cid);
           
           lblName.Text = row.Cells[0].Text;
           lblAddress.Text = row.Cells[1].Text;
           lblMobileNo.Text = Details[2];
            DateTime dt=Convert.ToDateTime ( Details[5]);
          //  lblAge.Text =Convert.ToString( DateTime.Now - dt);
            int age = (int)((DateTime.Now - dt).TotalDays / 365.255);
            lblAge.Text = age.ToString();
          //  DateTime dtt = DateTime.Now - dt;

                



            lblApply_For.Text=Details[1];
            lblApprochThru.Text=Details[16];
            lblEducation.Text=Details [7];
            lblEmailId.Text=Details[4];
            lblGender.Text=Details[6];
            lblGuardian_Contno.Text=Details[11];
            lblGuardianLoc.Text=Details[10];
            lblGuardianName.Text=Details[9];
            lblLocation.Text=Details[17];
            lblOccupa.Text=Details[14];
            lblQualify.Text=Details[8];
            lblRelationShip.Text=Details[12];
            
           lnkView_ModalPopupExtender.Show();
            
           
        
        }




        protected void lnkEdit_Click2(object sender, EventArgs e)
        {
            LinkButton lnkEdit = (LinkButton)sender;
          //  LinkButton lnkVw = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;
         //   int rowIndex = Convert.ToInt32(lnkEdit.CommandArgument);
            //AjaxControlToolkit.ModalPopupExtender m = (AjaxControlToolkit.ModalPopupExtender)lnkEdit.Parent.Parent.FindControl("lnkEdit_ModalPopupExtender");
            //m.TargetControlID = "lnkEdit";
            //m.Show();

            int Cid = Convert.ToInt32(gdCandidateProfile.DataKeys[row.RowIndex]["candidateId"]);

            string[] details = BAL.Customers.GetCandidateById(Cid);

          

            
            txtNamee.Text = details[0];

           // DropDownList dApply = (DropDownList)gvPanel.FindControl("ddlApplyFor");
           // BAL.Customers.PopulateCategory(ref dApply);
            ddlApplyFor.SelectedValue = details[1];



           // TextBox tMob = (TextBox)gvPanel.FindControl("txtMob");
            txtMob.Text = details[2];

           // TextBox tAdd = (TextBox)gvPanel.FindControl("txtAdd");
            txtAdd.Text = details[3];

            //TextBox tEmail = (TextBox)gvPanel.FindControl("");
            txtEmail.Text = details[4];

            //TextBox tdob = (TextBox)gvPanel.FindControl("txtDob");
            txtDob.Text = details[5];

            //RadioButtonList rGender = (RadioButtonList)gvPanel.FindControl("rdGender");
            rdGender.SelectedValue = details[6];

            //TextBox tEdu = (TextBox)gvPanel.FindControl("txtEducation");
            txtEducation.Text = details[7];

            //TextBox tQual = (TextBox)gvPanel.FindControl("txtQuali");
            txtQuali.Text = details[8];

            //TextBox tGNm = (TextBox)gvPanel.FindControl("txtGuaName");
            txtGuaName.Text = details[9];

            //TextBox tGLoc = (TextBox)gvPanel.FindControl("txtGuaLoc");
            txtGuaLoc.Text = details[10];

            //TextBox tGCo = (TextBox)gvPanel.FindControl("txtGuaCno");
            txtGuaCno.Text = details[11];

            //TextBox tRel = (TextBox)gvPanel.FindControl("txtGuaRela");
            txtGuaRela.Text = details[12];

            //TextBox tLoc = (TextBox)gvPanel.FindControl("txtLocation");
            txtLocation.Text = details[13];

            //Label lPathName = (Label)gvPanel.FindControl("lblPathName");
            lblPathName.Text = details[14];

            Session.Add("Oldpic", lblPathName.Text);

            //TextBox tOccu = (TextBox)gvPanel.FindControl("txtOccup");
            txtOccup.Text = details[15];


            //FileUpload profPic = (FileUpload)gvPanel.FindControl("FileUpload1");  
            //profPic.FileName=

            lnkEdit_ModalPopupExtender.Show();
        }

        protected void btnUpdate_Click2(object sender, EventArgs e)
        {
          //  Button update = (Button)sender;
          //  int rowIndex = Convert.ToInt32(update.CommandArgument);

           // Panel gvPanel = (Panel)gdCandidateProfile.Rows[rowIndex].Cells[0].FindControl("pnlLnkEdit");

           // TextBox tName = (TextBox)gvPanel.FindControl("");
            string name = txtNamee.Text;

            //TextBox tAdd = (TextBox)gvPanel.FindControl("");
            string add = txtAdd.Text;

          //  TextBox tmob = (TextBox)gvPanel.FindControl("");
            string mob = txtMob.Text;

            //TextBox tdob = (TextBox)gvPanel.FindControl("");
            DateTime dob = Convert.ToDateTime((txtDob.Text));

           // TextBox temail = (TextBox)gvPanel.FindControl("");
            string email = txtEmail.Text;

           // TextBox tedu = (TextBox)gvPanel.FindControl("");
            string edu = txtEducation.Text;

           // TextBox tqual = (TextBox)gvPanel.FindControl("");
            string qual = txtQuali.Text;

           // TextBox tGName = (TextBox)gvPanel.FindControl("");
            string GName = txtGuaName.Text;

           // TextBox tGLoc = (TextBox)gvPanel.FindControl("");
            string GLoc = txtGuaLoc.Text;

            //TextBox tGCo = (TextBox)gvPanel.FindControl("");
            string GCo = txtGuaCno.Text;




           // TextBox tRel = (TextBox)gvPanel.FindControl("");
            string GRel = txtGuaRela.Text;

            //TextBox tLoc = (TextBox)gvPanel.FindControl("");
            string Loc = txtLocation.Text;

            string oldpic = Convert.ToString(Session["Oldpic"]);
            //Label lPathName = (Label)gvPanel.FindControl("lblPathName");

           // FileUpload fpic = (FileUpload)gvPanel.FindControl("fuPic");

            // string Pic = fpic.FileName;
            // FileUpload profpic = (FileUpload)gvPanel.FindControl("");


            string profphoto = fuPic.FileName;
            if (profphoto == "")
            {
                profphoto = oldpic;
            }
            else
            {
                profphoto = fuPic.FileName;
                fuPic.SaveAs(Server.MapPath("ProfilePhoto/" + profphoto));
            }

           // RadioButtonList rd = (RadioButtonList)gvPanel.FindControl("rdGender");
            string gender = rdGender.SelectedValue;

           // DropDownList ddlApply = (DropDownList)gvPanel.FindControl("ddlApplyFor");
            string apply = ddlApplyFor.SelectedValue;

           // TextBox tOcc = (TextBox)gvPanel.FindControl("txtOccup");
            string Occup = txtOccup.Text;

            BAL.Customers.UpdateCandidate(name, add, Loc, mob, dob, gender, edu, qual, apply, GName, GCo, GLoc, GRel, profphoto, email, Occup);


            PopulateCandidateGrid();


        }





        protected void ddlSearchApplyFor_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ddlApplyFor = ddlSearchApplyFor.SelectedValue;
            if (ddlApplyFor != "0")
            {

                DataSet ds = BAL.Customers.getCandidateList(ddlApplyFor);
                gdCandidateProfile.DataSource = ds;
                gdCandidateProfile.DataBind();
            }
            else
            {
                PopulateCandidateGrid();
            }
        }


        protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            string location = ddlLocation.SelectedValue;
            if (location != "0")
            {

               
                 PopulateCandidateGrid();
            }


        }

    
        protected void btnSearch_Click2(object sender, EventArgs e)
        {
 string Search = txtName.Text;
            DataSet ds = BAL.Customers.GetCandidateByNameSearch(Search);
            gdCandidateProfile.DataSource = ds;
            gdCandidateProfile.DataBind();

        }

        protected void gdCandidateProfile_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdCandidateProfile.PageIndex = e.NewPageIndex;
            PopulateCandidateGrid();
        }



    }
}